<?php
use app\common\model\MoneyLog;
use app\common\model\GoodsNumberlog;

function getorderno($p = "")
{
    return $p . date("ymdHis") . str_pad(getMillisecond() % 1000, 4, "0", STR_PAD_LEFT) . random(4);
}
function inventory($goods_id,$number=null,$message=false){
    if($number===null){
        $goods=model("goods")->where("id",$goods_id)->find();
        return $goods['number'];
    }

    $number=intval($number);
    $goods=model("goods")->where("id",$goods_id)->lock(true)->find();
    if(empty($goods)){
        e("商品不存在,请刷新页面");
    }
    $currentnumber=intval($goods['number']);
    $endnumber=$currentnumber+$number;

    if($endnumber<0){
        if($message===false){
            return $goods['name']."库存不足";
        }
        e($goods['name']."库存不足");
    }
    if($message===false){
        return true;
    }
    if($number==0){
        return true;
    }
    $logdata=[];
    $logdata['goods_id']=$goods_id;
    $logdata['startnumber']=$currentnumber;
    $logdata['changenumber']=$number;
    $logdata['endnumber']=$endnumber;
    $logdata['datetime']=date("Y-m-d H:i:s");
    $logdata['message']=$message;
    $goodsgmodel=new GoodsNumberlog();
    $goodsgmodel->insert($logdata);
    model("goods")->update(['number'=>$endnumber],['id'=>$goods_id]);
    return true;
}



function refund($orderno){

    $order=model("order")->where("orderno",$orderno)->where("ispay",1)->find();
    if(empty($order)){
        e("订单状态不允许退款");
    }
    model("order")->where("orderno",$orderno)->update(["status"=>4]);
    return;
}
function money($user_id,$cmoney=null,$message=false){
    $lock=$message===false?false:true;
    $money=model("money")->where("user_id",$user_id)->lock($lock)->find();
    if(empty($money)){
        model("money")->save(['user_id'=>$user_id,"money"=>0]);
        $money=model("money")->where("user_id",$user_id)->find();
    }
    if($cmoney==null){
        return $money['money'];
    }
    $cmoney=intval($cmoney*100);
    if($cmoney==0){
        return;
    }
    $money=model("money")->where("user_id",$user_id)->lock($lock)->find();
    if(empty($money)){
        e("用户数据不存在,请刷新页面");
    }
    $currentmoney=intval($money['money']*100);
    $endmoney=$currentmoney+$cmoney;
    if($endmoney<0){
        if($message===false){
            return "用户余额不足";
        }
        e("用户余额不足");
    }
    if($message===false){
        return true;
    }
    $logdata=[];
    $logdata['user_id']=$user_id;
    $logdata['startmoney']=$currentmoney/100;
    $logdata['changemoney']=$cmoney/100;
    $logdata['endmoney']=$endmoney/100;
    $logdata['datetime']=date("Y-m-d H:i:s");
    $logdata['message']=$message;
    $usermoneylogmodel=new MoneyLog();
    $usermoneylogmodel->insert($logdata);
    model("money")->update(['money'=>$endmoney/100],['user_id'=>$user_id]);
    return true;

}

function integral($code,$ci,$message=null){
    $group=model("group")->where("code",$code)->find();
    if(empty($group)){
        return false;
    }
    $newi=intval($group['integral'])+intval($ci);
    if($newi<0){
        return false;
    }
    model("group")->where("code",$code)->update(['integral'=>$newi]);
    return true;
}
